function [] = cf_adder_tb()
    % http://www.ecs.umass.edu/ece/koren/arith/simulator/SDadd/
    
    r = 10;
    l = 9;
    u = 9;
    A = [4 5 3 6];
    B = [1 4 6 6];
    ansv = [6 0 0 2];
    S = cf_adder(A,B,r,l,u);
    assert(A,B,ansv,S,r,l,u);
    
    S = cf_adder(B,A,r,l,u);
    assert(B,A,ansv,S,r,l,u);
    
    r = 11;
    l = 10;
    u = 10;
    A = [0 9];
    B = [0 9];
    ansv = [1 7];
    S = cf_adder(A,B,r,l,u);
    assert(A,B,ansv,S,r,l,u);

    r = 11;
    l = 9;
    u = 11;
    A = [0 10];
    B = [0 10];
    ansv = [1 9];
    S = cf_adder(A,B,r,l,u);
    assert(A,B,ansv,S,r,l,u);
    
    r = 34;
    l = 33;
    u = 33;
    A = [15 29 30];
    B = [15 2 1];
    ansv = [30 31 31];
    S = cf_adder(A,B,r,l,u);
    assert(A,B,ansv,S,r,l,u);
    
    r = 35;
    l = 34;
    u = 34;
    A = [0 -29 30];
    B = [15 2 1];
    ansv = [15 -27 31];
    S = cf_adder(A,B,r,l,u);
    assert(A,B,ansv,S,r,l,u);
        
    r = 10;
    l = 7;
    u = 7;
    A = [2 3 -5 4];
    B = [5 6 -6 2];
    ansv = [1 -2 -2 -1 6];
    S = cf_adder(A,B,r,l,u);
    assert(A,B,ansv,S,r,l,u);

    r = 5;
    l = 2;
    u = 4;
    A = [4 -1 2 3];
    B = [-1 -2 3 4];
    ansv = [2 3 1 2];
    S = cf_adder(A,B,r,l,u);
    assert(A,B,ansv,S,r,l,u);
    
    r = 5;
    l = 4;
    u = 4;
    A = [4 -1 2 3];
    B = [-1 -2 3 4];
    ansv = [3 -2 1 2];
    S = cf_adder(A,B,r,l,u);
    assert(A,B,ansv,S,r,l,u);
    
    r = 13;
    l = 8;
    u = 8;
    A = [4 -1 2 3];
    B = [-1 -2 3 4];
    ansv = [3 -3 5 7];
    S = cf_adder(A,B,r,l,u);
    assert(A,B,ansv,S,r,l,u);
    
    r = 13;
    l = 8;
    u = 8;
    A = [4 -1 2 3];
    B = [-1 -2 3 4];
    ansv = [3 -3 5 7];
    S = cf_adder(A,B,r,l,u);
    assert(A,B,ansv,S,r,l,u);
    
    r = 4;
    l = 1;
    u = 5;
    A = [0 3 4 1 2 4 1 5];
    B = [1 2 3 2 0 0 5 3];
    ansv = [2 2 3 3 3 1 4 0];
    S = cf_adder(A,B,r,l,u);
    assert(A,B,ansv,S,r,l,u);

    r = 4;
    l = 3;
    u = 3;
    A = [2 -2 2 -3 -2 1 -1];
    B = [-3 2 -1 -3 -3 3 -1];
    ansv = [-1 0 0 -3 0 0 -2];
    S = cf_adder(A,B,r,l,u);
    assert(A,B,ansv,S,r,l,u);
    
    r = 4;
    l = 3;
    u = 3;
    A = [3 2 0];
    B = [1 0 3];
    ansv = [1 0 3 -1];
    S = cf_adder(A,B,r,l,u);
    assert(A,B,ansv,S,r,l,u);
    
    r = 4;
    l = 0;
    u = 5;
    A = [3 2 0];
    B = [1 0 3];
    ansv = [1 0 2 3];
    S = cf_adder(A,B,r,l,u);
    assert(A,B,ansv,S,r,l,u);

    r = 4;
    l = 0;
    u = 6;
    A = [5 1 5 0 1 4 2];
    B = [0 5 2 0 0 6 2];
    ansv = [1 2 3 3 0 3 2 4];
    S = cf_adder(A,B,r,l,u);
    assert(A,B,ansv,S,r,l,u);
    
    r = 7;
    l = 5;
    u = 6;
    A = [0 -4 -2 5 6 -4 -2];
    B = [5 -4 -4 0 4 -2 4];
    ansv = [4 -2 1 6 2 1 2];
    S = cf_adder(A,B,r,l,u);
    assert(A,B,ansv,S,r,l,u);
end

function [] = assert(A,B,ansv,S,r,l,u)
    a = sd2num(A);
    b = sd2num(B);
    s = sd2num(S);
    facit = sd2num(ansv);

    extra_check = sd2dec(A,r)+sd2dec(B,r) == sd2dec(S,r);
   
    A = mat2str(A);
    B = mat2str(B);
    ansv_ = mat2str(ansv);
    S_ = mat2str(S);
    
    fprintf('\textra_check: %d\n',extra_check);
    if(length(ansv) == length(S) && sum(ansv == S) == length(S))
        fprintf('\tYes! (%d + %d = %d)\n\n',a,b,s);
    else
        fprintf('\tNo ! correct is: %s (%d + %d = %d, correct is: %d)\n\n',ansv_,a,b,s,facit);
    end
end